﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace zmMoniter
{
    class DeckGrider
    {
        public Extent extent = null;
        public ArrayList ctlPoint = null;
        public ArrayList grdBlock = null;
        public int iCol = 0;
        public int iRow = 0;
        public double dGridLenth = 0.75;
        bool bIsLayerGrid = true;
        GraphicsPath myGraphicsPath;
        Region myRegion;
        public DeckGrider()
        {
            extent = new Extent();
            ctlPoint = new ArrayList();
            myGraphicsPath = new GraphicsPath();
            myRegion = new Region();
            myGraphicsPath.Reset();
        }
        public void clear()
        {
            
            extent.reSet();
            ctlPoint.Clear();
            if (grdBlock!=null)
                grdBlock.Clear();
            myGraphicsPath.Reset();
            myRegion.MakeEmpty();
            iCol = 0;
            iRow = 0;
        }
        public void addPoint(Point pt)
        {
            ctlPoint.Add(pt);
            extent.upDate(pt);
        }
        public void setReportDrid()
        {
            bIsLayerGrid = false;
        }
        public void setGridLength(double dLength)
        {
            dGridLenth=dLength;
        }
        public void setGridPoint()
        {
            if (ctlPoint.Count < 2)
                return ;
            Point pt1 = (Point)ctlPoint[0];
            Point ptn = (Point)ctlPoint[ctlPoint.Count - 1];
            if (!pt1.compare(ptn))
            {
                addPoint(pt1);
            }
            int iLength = ctlPoint.Count;
            System.Drawing.PointF[] pts = new System.Drawing.PointF[iLength];
            for (int j = 0; j < iLength; j++)
            {
                Point pt = (Point)ctlPoint[j];

                pts[j] = new PointF((float)pt.dX * 100, (float)pt.dY*100);
            }
            myGraphicsPath.AddPolygon(pts);
            myRegion.MakeEmpty();
            myRegion.Union(myGraphicsPath);
            if (grdBlock == null)
                grdBlock = new ArrayList();
            else
                grdBlock.Clear();
            iCol = (int)Math.Ceiling(extent.getWidth() / dGridLenth);
            iRow = (int)Math.Ceiling(extent.getHeight() / dGridLenth);
            for (int i = 0; i <= iRow; i++)
            {
                for (int j = 0; j <= iCol; j++)
                {
                    double x = extent.dMinX + dGridLenth / 2 + j * dGridLenth;
                    double y = extent.dMaxY - dGridLenth / 2 - i * dGridLenth;
                    if (IsPointInRegion(x , y))
                    {
                       Grid grd = new Grid((float)x, (float)y);
                       grd.setOverLay(0);
                       grdBlock.Add(grd);
                    }
                    else if (!bIsLayerGrid)
                    {
                        Grid grd = new Grid((float)x, (float)y);
                        grdBlock.Add(grd);
                    }
                }
            }
        }
        public string getGridPoint()
        {
            string sReslue="";
            if (ctlPoint.Count < 2)
                return sReslue;
            Point pt1 = (Point)ctlPoint[0];
            Point ptn = (Point)ctlPoint[ctlPoint.Count - 1];
            if (!pt1.compare(ptn))
            {
                addPoint(pt1);
            }

            int iLength = ctlPoint.Count;
            System.Drawing.PointF[] pts = new System.Drawing.PointF[iLength];
            for (int j = 0; j < iLength; j++)
            {
                Point pt = (Point)ctlPoint[j];

                pts[j] = new PointF((float)pt.dX * 100, (float)pt.dY * 100);
            }
            myGraphicsPath.AddPolygon(pts);
            myRegion.MakeEmpty();
            myRegion.Union(myGraphicsPath);

            int iCol = (int)Math.Ceiling(extent.getWidth() / dGridLenth);
            int iRow = (int)Math.Ceiling(extent.getHeight() / dGridLenth);
            for (int i = 0; i <= iRow; i++)
            {
                for (int j = 0; j <= iCol; j++)
                {
                    double x = extent.dMinX + j * dGridLenth;
                    double y = extent.dMaxY - i* dGridLenth;
                    if (IsPointInRegion(x, y))
                    {
                        sReslue += x + "," + y + ",0,0001-01-01 00:00:00;";
                    }
                 
                }
            }
            if (sReslue != "")
                sReslue = sReslue.Substring(0, sReslue.Length - 1);
            return sReslue;
        }
        public bool IsPointInRegion(double dX, double dY)
        {
            PointF ptf = new PointF((float)dX * 100, (float)dY * 100);
            return myRegion.IsVisible(ptf);                  
        }
    }
}
